/**
 * Created by lanyanhua on 2018/10/31 0031.
 */
NProgress.start();
window.onload = function () {
    NProgress.done();
};
layui.use(['element', 'table', 'form', 'jquery', 'laydate','list'], function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.jquery;
    var laydate = layui.laydate;
    var list = layui.list;

    laydate.render({
        elem: '#startTime',
        type: "datetime"
    });

    laydate.render({
        elem: '#endTime',
        type: "datetime"
    });

    var showdatatable =  table.render({
        elem: '#tableId',
        id:'showdata',
        url: '/jobGroup/findJobGroupPage',
        limit: 10,
        page: true,
        while: param(),
        cols: [ [
            {type: 'checkbox'},
            {field: 'groupNo', title: '项目编号',sort: true},
            {field: 'groupName', title: '项目名称'},
            {field: 'introduce', title: '项目介绍'},
            {title: '操作', templet: '#operationTpl', align: 'center'}
        ] ],
        done: function (res, curr, count) {
            $("#countNum").text(count);
        }
    });
    //list.open(options); 表单弹框
    var listOpen  = {
        elem:"#groupAdd",
        fields:{
            groupNo : {label:"项目编号",verify:'required|exists',url:'/jobGroup/existsById'},
            groupName : {label:"项目名称"},
            introduce:{label:"项目介绍",type:"textarea" }
        },
        submit : function(res){ //表单提交事件
            $.ajax({
                type : 'post',
                url : '/jobGroup/saveJobGroup',
                data : res.field ,
                dataType : 'json',
                success : function (data) {
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        var index = layer.getFrameIndex(window.name);
                        layer.close(index); //再执行关闭
                        layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                }
            });
            return false;
        },
        btns:["submit",'reset']
    };


    form.on('submit(search)', function () {
        showdatatable.reload({where: param()});
        return false;
    });

    table.on('tool(tableFilter)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            //console.log(data);
            listOpen.fields.groupNo.readonly="readonly";
            listOpen.fields.groupNo.verify = 'required';
            listOpen.over = data;
            list.open(listOpen);
        } else if (layEvent === 'del') {
            layer.confirm("确定要删除吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                layer.msg("操作成功！", {icon: 1, time: 1000});
            });
        }
    });
    $("#batchDel").click(function () {
        layer.confirm("确定要批量删除吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
            var checkStatus = table.checkStatus('tableId');
            var rows = checkStatus.data.length;
            if (rows > 0) {
                var idsStr = "";
                for (var i = 0; i < checkStatus.data.length; i++) {
                    idsStr += checkStatus.data[i].id + ',';
                }
                console.log("选择的id-->" + idsStr);
                layer.msg("操作成功！", {icon: 1, time: 1000}, function () {
                    $(".layui-laypage-btn")[0].click();
                });
            } else {
                layer.msg('未选择有效数据', {
                    offset: 't',
                    anim: 6
                });
            }
        });
    });
    // 添加
    $("#addGroup").click(function(){
        listOpen.fields.groupNo.readonly=null;
        listOpen.fields.groupNo.verify = 'required|exists';
        listOpen.over = null;
        list.open(listOpen);

    });
    function  param() {
        return {
            groupName: $('[name=groupName]').val(),
            groupNo: $('[name=groupNo]').val()
        }
    }
});
